package code.assets
{
	import code.interfaces.IContainerObject3D;
	import code.interfaces.ITextSurface;
	
	import flash.events.EventDispatcher;
	
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.scenes.Scene3D;

	public class LoadTextCubes extends LoadScene
	{
		public function LoadTextCubes(topLevel:XML, scene:Scene3D, w:uint, d:uint)
		{
			for each(var element:XML in topLevel.li.a)
			{
				scene.addChild(getContainerObject3D(element, w, d) as DisplayObject3D);
			}
			
			//trace(topLevel);
			
		}
		
		protected function getContainerObject3D(topLevel:XML, w:uint, d:uint):IContainerObject3D
		{	
			var clip:TextSurface;
			var object3D:IContainerObject3D;
			var ml:MaterialsList;
			var mMat:MaterialObject3D;
	
			ml = new MaterialsList();
			for(var i:uint=0; i<6; i++)
			{	
				
				switch(i)
				{
					case 0:
					{
						mMat = new ColorMaterial(0xFFFFFF);
						ml.addMaterial(mMat, "top");
						break;
					}
					case 1:
					{
						mMat = new ColorMaterial(0xFFFFFF);
						ml.addMaterial(mMat, "bottom");
						break;
					}
					case 2:
					{
						clip = new TextSurface();
						clip.width = w;
						//clip.height = _height;
						(clip as ITextSurface).linkID = topLevel.@id;
						clip.createLabel(topLevel, 0xf1f1f1);
						mMat = new MovieMaterial(clip);
						ml.addMaterial(mMat as MovieMaterial, "back");
						break;
					}
					case 3:
					{
						clip = new TextSurface();
						clip.width = w;
						//clip.height = _height;
						(clip as ITextSurface).linkID = topLevel.@id;
						clip.createLabel(topLevel, 0xa4a4a4);
						mMat = new MovieMaterial(clip);
						ml.addMaterial(mMat as MovieMaterial, "right");
						break;
					}
					case 4:
					{
						clip = new TextSurface();
						clip.width = w;
						//clip.height = _height;
						(clip as ITextSurface).linkID = topLevel.@id;
						clip.createLabel(topLevel, 0xa4a4a4);
						mMat = new MovieMaterial(clip);
						ml.addMaterial(mMat, "left");
						break;
					}
					case 5:
					{
						mMat = new ColorMaterial(0x99FFFF);
						ml.addMaterial(mMat, "front");
						break;
					}
				}
				
				var h:uint;
				if(clip)
				{
					clip.width = w;
					//clip.height = _height;
					(clip as ITextSurface).linkID = topLevel.@id;
					h = clip.textWidth;
				}
				
				mMat.interactive = true;
				mMat.smooth = true;
			}
			
			object3D = new ContainerCube3D(ml, w, d, h, 6, 6, 6);
			(object3D as Cube).y = h/2;
			object3D.linkID = topLevel.@id;
			
			return object3D;
		}
		
	}
}